
import java.awt.*;  
import java.awt.geom.Line2D;
import java.util.ArrayList;

import javax.swing.*;  

public class Plot extends JPanel {  
    ArrayList<double[]> yData = new ArrayList<double[]>();
    ArrayList<double[]> xData = new ArrayList<double[]>();
    String xName, yName;
    final int PAD = 30;  
    boolean fullScreen = false;
    
    public void setParameters(String xName, double [] xData, String yName, double[] yData){
    	
    	this.yData.add(yData);
    	this.xData.add(xData);
    	
    	this.xName = xName;
    	this.yName = yName;
    	
    }
    
    public void addData(double [] xData, double[] yData){
    	
    	this.yData.add(yData);
    	this.xData.add(xData);
    	
    }
   
    protected void paintComponent(Graphics g) {  
    	
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        int w = getWidth();  
        int h = getHeight();  
        
        g2d.drawLine(PAD, PAD, PAD, h-PAD);  
        g2d.drawLine(PAD, h-PAD, w-PAD-20, h-PAD);  
        
        
        g2d.drawString(yName, PAD-20, PAD-5);
        g2d.drawString(xName, w-PAD-50, h-PAD-5);
        
        Color[] colors = new Color[]{Color.red,Color.blue,Color.green,Color.yellow};
        
        
        for(int i = 0; i < yData.size(); i++){
        	
        	double[] db = yData.get(i);
        	
            double xScale = (w - 2*PAD)/(db.length + 1);  
            double maxValue = 100;
            double yScale = (h - 2*PAD)/(maxValue + 10);  
            
            for(int y = 0; y < 101; y += 10){
            	
            	g2d.drawLine(PAD-5, (h-PAD)-(int)(y*yScale), PAD, (h-PAD)-(int)(y*yScale));
            	g2d.drawString("" + y, PAD-25, (h-PAD)-(int)(y*yScale));
            	
            }
            
            // The origin location.  
            int x0 = PAD;  
            int y0 = h-PAD;
            int x1 = x0, y1 = y0, x2, y2;
            
            for(int j = 0; j < db.length; j++) {  
            	
            	if(i < colors.length)
            		g2d.setPaint(colors[i]);  
            	
            	x2 = x0 + (int)(xScale * (j));  
                y2 = y0 - (int)(yScale * db[j]); 
                
                Line2D line = new Line2D.Double();
                
                line = new Line2D.Double(x1, y1, x2, y2);
                
                x1 = x2;
                y1 = y2;
                
                g2d.fillOval(x2-2, y2-2, 4, 4);  
                g2d.draw(line);
                
                g2d.setPaint(Color.BLACK);
                
                
                g2d.drawLine(x2, h-PAD, x2, h-PAD+5);
                
                g2d.drawString("" + xData.get(i)[j], x2-5, h-PAD+20);
            
            }
        	
        }    
        
    }  
    
    
    
    
    public double getmax(double[] list){
    	
    	double max = 0.0;
    	
    	for(int i = 0; i < list.length; i++)
    		if(list[i] > max)
    			max = list[i];
    	
    	return(max);
    	
    }
    
   
} 